<template>
  <el-card>
    <div slot="header" class="clearfix">
      <el-form :inline="true" :model="query">
        <el-button type="primary" round @click="dialog.scheduleShow = true"
          >巡检时间</el-button
        >
        <el-form-item>
          <el-input placeholder="请输入内容" prefix-icon="el-icon-search">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            round
            plain
          ></el-button>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="success" @click="dialog.addShow = true" round
            >新建</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="inList" border style="width: 100%"
    :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ 'text-align': 'center' }">
      <el-table-column prop="inspectionRecordCode" label="巡检记录编码">
      </el-table-column>
      <el-table-column prop="inspectionTime" label="规定巡检时间">
      </el-table-column>
      <el-table-column prop="inspectionRecordTime" label="实际巡检时间">
      </el-table-column>
      <el-table-column prop="nickName" label="巡检人"> </el-table-column>
      <el-table-column prop="inspectionRecordRemark" label="备注">
        <template slot-scope="scope">
          {{
            scope.row.inspectionRecordRemark == null
              ? "无"
              : scope.row.inspectionRecordRemark
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300px">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            @click="handleEdit(scope.row.inspectionRecordId)"
            >编辑</el-button
          >
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row.inspectionRecordId)"
          >
            <el-button
              type="danger"
              slot="reference"
              size="small"
              icon="el-icon-delete"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="query.currentPage"
      :page-sizes="pageInfo.pageSizes"
      :page-size="query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
    >
    </el-pagination>
    <!-- -------------------修改dialog---------------------- -->
    <el-dialog title="修改记录" :visible.sync="dialog.updateShow" width="30%">
      <el-form :model="inspInfo.editInfo">
        <el-form-item label="巡检记录编号" :label-width="formLabelWidth">
          <el-input
            v-bind:value="inspInfo.editInfo.inspectionRecordCode"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="规定巡检时间" :label-width="formLabelWidth">
          <el-input
            v-model="inspInfo.editInfo.inspectionTime"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="实际巡检时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="inspInfo.editInfo.inspectionRecordTime"
            type="datetime"
            style="width: 100%"
            disabled
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="巡检人" :label-width="formLabelWidth">
          <el-cascader
            v-model="optionList"
            :options="options"
            @change="handleOptionChange"
            style="width: 100%"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            rows="3"
            v-model="inspInfo.editInfo.inspectionRecordRemark"
            autocomplete="off"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>
        <el-form-item label="id" :label-width="formLabelWidth" hidden>
          <el-input v-model="inspInfo.editInfo.inspectionRecordId"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.updateShow = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
    <!-- -------------------添加dialog---------------------- -->
    <el-dialog title="添加记录" :visible.sync="dialog.addShow" width="30%">
      <el-form :model="inspInfo.addInfo">
        <el-form-item label="规定巡检时间" :label-width="formLabelWidth">
          <el-select
            v-model="inspInfo.addInfo.inspectionScheduleId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in schedules"
              :key="item.inspectionScheduleId"
              :label="item.inspectionTime"
              :value="item.inspectionScheduleId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际巡检时间" :label-width="formLabelWidth">
          <el-date-picker
            v-model="inspInfo.addInfo.inspectionRecordTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="巡检人" :label-width="formLabelWidth">
          <el-cascader
            v-model="optionList"
            :options="options"
            @change="handleOptionChange"
            style="width: 100%"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input
            type="textarea"
            rows="3"
            v-model="inspInfo.addInfo.inspectionRecordRemark"
            autocomplete="off"
            placeholder="请输入内容"
          ></el-input>
        </el-form-item>

        <el-form-item label="id" :label-width="formLabelWidth" hidden>
          <el-input v-model="inspInfo.addInfo.inspectionRecordId"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog.addShow = false">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 巡检时间表dialog -->
    <el-dialog
      title="巡检时间表"
      :visible.sync="dialog.scheduleShow"
      width="30%"
    >
      <el-table :data="schedules" style="width: 100%">
        <el-table-column prop="inspectionTime" label="巡检时间" style="width:100%;">
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="loadSchedules">关闭</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      schedules: [],
      options: [],
      optionList: [],
      dialog: {
        scheduleShow: false,
        updateShow: false,
        addShow: false,
      },
      inspInfo: {
        editInfo: {},
        addInfo: {},
      },
      query: {
        currentPage: 1,
        pageSize: 5,
      },
      inList: [],
      pageInfo: {
        pageSizes: [5, 10, 30, 50],
        total: 0,
      },
      formLabelWidth: "120px",
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  methods: {
    add() {
      this.inspInfo.addInfo.workNo =
        this.optionList[this.optionList.length - 1];
      this.$axios.post("/irs", this.inspInfo.addInfo).then((res) => {
        if (res.data.code == 200) {
          this.$message.success("添加成功");
          this.dialog.addShow = false;
          this.loadRecords();
        } else {
          this.$message.success("添加失败");
        }
        this.optionList = [];
      });
    },
    handleOptionChange() {
      this.inspInfo.editInfo.workNo =
        this.optionList[this.optionList.length - 1];
    },
    handleSizeChange(val) {
      this.query.pageSize = val;
      this.loadRecords();
    },
    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.loadRecords();
    },
    loadRecords() {
      this.$axios.post("/irs/queryAll", this.query).then((res) => {
        this.pageInfo.total = res.data.data.total; //满足条件的总条数
        this.inList = res.data.data.list; //当前页的数据
      });
    },
    loadSchedules() {
      this.dialog.scheduleShow = false;
      this.$axios.get("/iss").then((res) => {
        this.schedules = res.data.data.list;
      });
    },
    handleEdit(id) {
      this.$axios.get("/irs/" + id).then((res) => {
        if (res.data.code == 200) {
          this.dialog.updateShow = true;
          this.inspInfo.editInfo = res.data.data.inspectionRecord;
          this.optionList = [];
          this.optionList.push(this.inspInfo.editInfo.deptId + "");
          this.optionList.push(this.inspInfo.editInfo.jobId + "");
          this.optionList.push(this.inspInfo.editInfo.workNo);
        } else {
          this.$message.error(res.data.message);
        }
      });
    },

    update() {
      this.$axios.put("/irs", this.inspInfo.editInfo).then((res) => {
        this.dialog.updateShow = false;
        if (res.data.code == 200) {
          this.loadRecords();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    loadOptions() {
      this.$axios
        .post("/emps/3")
        .then((res) => {
          if (res.data.code == 200) {
            this.options = res.data.data.vos;
          }
        })
        .catch((err) => {
          console.error(err);
        });
    },
  },
  mounted() {
    this.loadRecords();
    this.loadOptions();
    this.loadSchedules();
  },
};
</script>
